package com.sinosoft.shopcar;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.base.GenericManagerImpl;
import com.sinosoft.orderinfomodel.OrderInfoModelManager;
import com.sinosoft.product.ProductSearchManager;
import com.sinosoft.util.PropertiesUtil;

@Service("shopcarManager")
public class ShopcarManagerImp extends GenericManagerImpl<Shopinfo, Long> implements ShopcarManager {
	ShopcarDao shopdao;
	ProductSearchManager productmgr;
	OrderInfoModelManager omodelmgr;
	
	@Autowired
	public void setProductSearchManager(ProductSearchManager productmgr)
	{
		this.productmgr = productmgr;
	}
	@Autowired
	public void setOrderInfoModelManager(OrderInfoModelManager omgr)
	{
		this.omodelmgr = omgr;
	}
	@Autowired
    public ShopcarManagerImp(ShopcarDao shopdao) {
        super(shopdao);
        this.shopdao = shopdao;
    }
	
	public List<Shopinfo> search(String searchTerm) {
		return super.search(searchTerm, Shopinfo.class);
	}
	
	public List<Shopinfo> getShopcarList(String username,int pagesize,int page)
	{
		return shopdao.getShopcarList(username,pagesize,page);
	}
	
	public Shopinfo saveShopcar(Shopinfo shopinfo) {
		
		return shopdao.saveShopcar(shopinfo);
	}

	public void removeShopcarlist(List<Shopinfo> shoplist) {
		shopdao.removeShopcarlist(shoplist);
	}
	
	public List<Shopinfo> getShopinfoListByIds(String ids)
	{
		return shopdao.getShopinfoListByIds(ids);
	}
	
	public void removeShopcar(String id)
	{
		shopdao.removeShopcar(id);
	}
	
	public Shopinfo getShopinfoById(int id)
	{
		return shopdao.getShopinfoById(id);
	}

	public int getShopcarCount(String username) {
		StringBuffer sql = new StringBuffer("select count(*) from zywx_shop_info");
		if(username!=null && !username.isEmpty())
			sql.append(" where username='"+username+"'");
		return shopdao.getShopcarCount(sql.toString());
	}

	public List<Shopinfo> getShopcarListByUsername(String username,String orderids) {
		return shopdao.getShopcarListByUsername(username,orderids);
	}
	
	public String SaveShopcarList(List<Shopinfo> shopinfolist)
	{
		StringBuffer jsonstr = new StringBuffer("[");
		Shopinfo s = null;
		int length = 0;
		for (Shopinfo shop : shopinfolist) {
			length ++;
			s = new Shopinfo();
			s = shopdao.saveShopcar(shop);
			jsonstr.append("{");
			
			jsonstr.append("\"sceneid\":\"" + s.getSceneid() + "\",");
			jsonstr.append("\"tasktype\":\"" + s.getTasktype() + "\",");
			jsonstr.append("\"satelliteid\":\"" + s.getSatelliteid() + "\",");
			jsonstr.append("\"sensorid\":\"" + s.getSensorid() + "\",");
			jsonstr.append("\"orderid\":\"" + s.getOrderid() + "\",");
			jsonstr.append("\"productlevel\":\"" + s.getProductlevel() + "\"");
			
			if(length!=shopinfolist.size()){
				jsonstr.append("},");
			}
			else{
				jsonstr.append("}");
			}
		}
		jsonstr.append("]");
		return jsonstr.toString();
	}
	
	/**
	 * @param level 产品级别
	 * @param keys([satellite_sensorid_productid,satellite1_sensorid1_productid1])
	 * @return
	 */
	public List<Shopinfo> saveShopcarListByProductInfo(final String level, final String keys)
	{
		String[] tablenames = PropertiesUtil.getPropertiesValueByFilename("zywximage.properties", level);
		String tablename = tablenames==null||tablenames.length<1?null:tablenames[0];
		List<Object> objlist = productmgr.getProducts(tablename,keys);
		List<Shopinfo> shopinfolist = new ArrayList<Shopinfo>();
		List<Shopinfo> shopinfolists = new ArrayList<Shopinfo>();
		shopinfolist = SwitchToShopcar.getShopinfo(objlist,omodelmgr,null);
		for (Shopinfo shop : shopinfolist) {
			Shopinfo s = shopdao.saveShopcar(shop);
			shopinfolists.add(s);
		}
		return shopinfolists;
	}
}
